/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import abstractmodel.*;
import java.awt.Component;
import java.awt.Window;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Detail;

/**
 *
 * @author littlechild
 */
public class Utilities {

    public static void cancelDialog(Component c) {
        c = c.getParent();
        while (!(c instanceof Window)) {
            c = c.getParent();
        }
        ((Window) c).dispose();
    }
    
    public static String getMd5(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            MessageDigest change = (MessageDigest) md.clone();
            byte[] changeBytes = change.digest();
            String md5Password = new String(Arrays.toString(changeBytes));
            return md5Password;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * 
     * @param account 需要添加明细的帐户
     * @param filename 明细文件名
     * @return 添加明细成功与否
     */
    public static boolean inputDetailFromFile(IAccount account, String filename) {
        FileInputStream is = null;
        try {
            is = new FileInputStream(filename);
            InputStreamReader ir = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(ir);

            //保存读出的每一行明细
            String[] titleNamesString = null;
            if (account instanceof model.BankAccount || account instanceof model.MoneyAccount) {
                titleNamesString = model.DetailInfo.titles;
            } else {
                titleNamesString = model.CreditCardDetailInfo.creditCardDetailInfoTitles;
            }

            //s遍历每一行
            String s = null;

            while ((s = in.readLine()) != null) {
                System.out.println(s);
                String[] detailString = s.split("[|]");     //按照|分割
                System.out.println(Arrays.toString(detailString));
                IDetail d = Main.mf.newDetail(account);
                IInfo info = d.getDetailInfo();
                for (int i = 0; i < detailString.length; i++) {
                    switch (i) {
                        case 0: 
                        case 1: info.setValue(titleNamesString[i], detailString[i]);
                                    break;
                        case 3:
                        case 4: info.setValue(titleNamesString[i + 1], detailString[i]);
                                    break;
                        case 2:  info.setValue(titleNamesString[i], new Double(detailString[i]));
                                    break;
                    }
                }
                d.setDetailInfo(info);
                account.addDetail(d);
                account.addDetail(d);
            }
            return true;
        } catch (IOException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
